<?php
function validate($rData, $sMode, $bRemoveEmpty=false) {
   $rVal = array();
   foreach (explode('|', trim($sMode, ' |')) as $sField) {
      $r = explode(':', $sField);
      if (2 > sizeof($r)) { list($sKey, $sFormat, $sKeyNew) = $r; }
      else { list($sKey, $sFormat) = $r; $sKeyNew = $sKey; }
      if (!isset($rData[$sKey])) { $rData[$sKey] = false; }
      switch ($sFormat{0}) {
      case 'n': // integer > 0
      case 'N':
         $x = max(0, (int)$rData[$sKey]);
         if (strtoupper($sFormat{0}) === $sFormat{0} && !$x) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = $x; }
         break;
      case 'i': // integer
      case 'I':
         $x = $rData[$sKey];
         if (strtoupper($sFormat{0}) === $sFormat{0} && !is_numeric($x)) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = (int)$x; }
         break;
      case 'd': // YYYY-mm-dd
      case 'D':
         if (preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2})/', $rData[$sKey], $r)) { $rVal[$sKeyNew] = $r[1]; }
         elseif (strtoupper($sFormat{0}) === $sFormat{0}) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = '0000-00-00'; }
         break;
      case 't': // YYYY-mm-dd HH:ii:ss
      case 'T':
         if (preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})/', $rData[$sKey], $r)) { $rVal[$sKeyNew] = $r[1]; }
         elseif (preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2})/', $rData[$sKey], $r)) { $rVal[$sKeyNew] = $r[1].':00'; }
         elseif (preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2})/', $rData[$sKey], $r)) { $rVal[$sKeyNew] = $r[1].' 00:00:00'; }
         elseif (strtoupper($sFormat{0}) === $sFormat{0}) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = '0000-00-00 00:00:00'; }
         break;
      case 'e': // enum
      case 'E':
         $rEnum = explode(',', trim(substr($sFormat, 1), '[]'));
         $x = trim($rData[$sKey]);
         if (in_array($x, $rEnum)) { $rVal[$sKeyNew] = $x; }
         elseif (strtoupper($sFormat{0}) === $sFormat{0}) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = $rEnum[0]; }
         break;
      case 's': // all string
      case 'S':
         $x = trim($rData[$sKey]);
         if (strtoupper($sFormat{0}) === $sFormat{0} && !$x) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = $x; }
         break;
      case 'g': // iso or gbk string
      case 'G':
         $x = trim($rData[$sKey]);
         if (strtoupper($sFormat{0}) === $sFormat{0} && !$x) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = $x; }
         break;
      case 'u': // utf8 string
      case 'U':
         $x = trim($rData[$sKey]);
         $x = preg_replace_callback("/%u([0-9a-f]{3,4})/i", create_function(
            '$r', 'return html_entity_decode("&#x{$r[1]};", null, "UTF-8");'
         ), $x);
         $x = iconv('utf8', 'gbk', $x);
         if (strtoupper($sFormat{0}) === $sFormat{0} && !$x) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = $x; }
         break;
      case 'r': // array
      case 'R':
         $x = $rData[$sKey];
         if (strtoupper($sFormat{0}) === $sFormat{0} && (!is_array($x) || !sizeof($x))) { if ($bRemoveEmpty) { break; } else { return false; } }
         else { $rVal[$sKeyNew] = (!is_array($x) || !sizeof($x)) ? array() : $x; }
         break;
      default:
         if (!$bRemoveEmpty || $rData[$sKey]) { $rVal[$sKeyNew] = $rData[$sKey]; }
         break;
      }
   }
   return $rVal;
}

/// get IP ///
function get_remote_addr(){
   $ip = NULL;
   if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
   }
   else if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
      $ip = $_SERVER['HTTP_CLIENT_IP'];
   }
   else if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
      $ip = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
   }
   else if (!empty($_SERVER['HTTP_X_FORWARD_FOR'])) {
      $ip = $_SERVER['HTTP_X_FORWARD_FOR'];
   }
   else if (!empty($_SERVER['REMOTE_ADDR'])) {
      $ip = $_SERVER['REMOTE_ADDR'];
   }
   $_ = explode(',', $ip);
   $ip = trim($_[0]);
   return $ip;
}
?>
